// pages/newAddress/index.js
Page({
  address: function () { //取本地缓存赋值
    var addressList = wx.getStorageSync('addressList')
    if (addressList) {
      this.setData({
        list: addressList
      })
    } else {
      this.setData({
        list: []
      })
    }
  },
  onChange(event) {
    var index = event.currentTarget.dataset.index
    var arr = wx.getStorageSync('addressList')
    arr.map((item, indexs) => {
      item.flag = false
      if (indexs == index) {
        item.flag = true
      }
    })
    wx.setStorageSync('addressList', arr);
    var list = wx.getStorageSync('addressList')
    this.setData({
      list: list
    });
  },
  addNewAddress: function () {
    wx.navigateTo({
      url: '../address/index',
    })
  },
  editor: function (e) {
    var obj = e.currentTarget.dataset.item;
    obj.index = e.currentTarget.dataset.index;
    wx.navigateTo({
      url: '../address/index',
      success: (res) => {
        res.eventChannel.emit('para', obj)
      }
    })
  },
  delect: function (index) {
    var arr = wx.getStorageSync('addressList')
    arr.map((item, indexs) => {
      if (indexs === index) {
        arr.splice(indexs, 1);
      }
    })
    this.setData({
      list:arr
    })
    wx.setStorageSync('addressList', arr);
  },
  del: function (e) {
    var index = e.currentTarget.dataset.index;
    this.setData({
      index: index,
      showOneButtonDialog: true,
    })
  },
  tapDialogButton(e) {
    if (e.detail.item.text == '确定') {
      this.setData({
        showOneButtonDialog: false
      })
      this.delect(this.data.index)
    } else {
      this.setData({
        showOneButtonDialog: false
      })
    }
  },
  /**
   * 页面的初始数据
   */
  data: {
    radio: true,
    list: null,
    showOneButtonDialog: null,
    buttons: [{ text: '取消' }, { text: '确定' }],
    index: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.address()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})